package com.test.javafaq_portal.service.impl;

import com.test.javafaq_portal.entity.Comment;
import com.test.javafaq_portal.entity.User;
import com.test.javafaq_portal.mapper.CommentMapper;
import com.test.javafaq_portal.mapper.UserMapper;
import com.test.javafaq_portal.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.javafaq_portal.service.ServiceException;
import com.test.javafaq_portal.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author test.com
 * @since 2025-04-11
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    UserMapper userMapper;

    @Override
    @Transactional
    public Comment saveComment(CommentVo commentVo, String username) {
        User user = userMapper.findUserByUsername(username);
        Comment comment = new Comment().setUserId(user.getId())
                .setAnswerId(commentVo.getAnswerId())
                .setContent(commentVo.getContent())
                .setCreatetime(LocalDateTime.now())
                .setUserNickName(user.getNickname());
        boolean save = save(comment);
        if(!save){
            throw ServiceException.busy();
        }
        return comment;
    }
}
